#include "RoundOffOperationWidget.h"
#include "Core/MainWindow.h"
#include <QMessageBox>
#include <BRepFilletAPI_MakeFillet.hxx>
#include <TopoDS.hxx>
#include "FeatureShape/HistoryManager.h"
#include "FeatureShape/FeatureShape.h"
#include <TopExp_Explorer.hxx>


RoundOffOperationWidget::RoundOffOperationWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRSWidgetBase(inMainWindow, inOperation, parent)
	, ui(new Ui::RoundOffOperationWidgetClass())
{
	ui->setupUi(this);

	connect(m_mainWindow, &MainWindow::OnSelectedSubShapes, this, &RoundOffOperationWidget::OnSelectedSubShapes);
	connect(ui->modifyButton, &QPushButton::clicked, this, &RoundOffOperationWidget::OnModify);
	connect(ui->confirmButton, &QPushButton::clicked, this, &RoundOffOperationWidget::OnConfirm);
	connect(ui->cancelButton, &QPushButton::clicked, this, &RoundOffOperationWidget::close);

	ui->confirmButton->setEnabled(false);
}

RoundOffOperationWidget::~RoundOffOperationWidget()
{
	delete ui;
}

void RoundOffOperationWidget::closeEvent(QCloseEvent* event)
{
	disconnect(m_mainWindow, &MainWindow::OnSelectedSubShapes, this, &RoundOffOperationWidget::OnSelectedSubShapes);

	if (!bIsConfirmed)
	{
		if (cacheActor)
		{
			m_mainWindow->RemoveActorFromRenderer(cacheActor);
			cacheActor = nullptr;
		}

		if (m_selectedActor)
		{
			m_selectedActor->SetPipelineVisibility(true);
		}
	}

	m_mainWindow->SubWidgetClose(bIsConfirmed);
}

void RoundOffOperationWidget::OnSelectedSubShapes(QIVtkSelectionPipeline* actor)
{
	if (!ui->modifyButton->isEnabled())
	{
		return;
	}

	m_selectedActor = actor;
	m_shapelist = m_mainWindow->GetSelectedShapes();
}

void RoundOffOperationWidget::OnModify()
{
	if (m_selectedActor.IsNull())
	{
		QMessageBox::warning(this, "警告", "无效倒角对象");
		return;
	}

	if ((IsNearlyZero(ui->radius->value())))
	{
		QMessageBox::warning(this, "警告", "请输入有效的倒角半径");
		return;
	}

	BRepFilletAPI_MakeFillet filletMaker(m_selectedActor->OccShape());
	edgesToRoundCache.clear();
	for (const auto& it : m_shapelist)
	{
		TopoDS_Edge edge = TopoDS::Edge(it);
		if (edge.IsNull())
		{
			QMessageBox::warning(this, "警告", "选择了非边的拓扑形状");
			return;
		}
        else
		{
			filletMaker.Add(ui->radius->value(), edge);
			edgesToRoundCache.push_back(edge);
		}
	}

    filletMaker.Build();
	if (filletMaker.Shape().IsNull())
	{
		QMessageBox::warning(this, "警告", "倒圆角失败");
		return;
	}

	cacheActor = m_mainWindow->createBasicUnitActor(filletMaker.Shape());
	ui->confirmButton->setEnabled(true);
	ui->modifyButton->setEnabled(false);

	m_selectedActor->SetPipelineVisibility(false);
	_radius = ui->radius->value();
}

void RoundOffOperationWidget::OnConfirm()
{
	bIsConfirmed = true;

	int shapeIndex = GetIndexFromActor(m_mainWindow->GetCachedActors(), m_selectedActor);
	std::vector<int> edgeIndex;
	TopExp_Explorer explorer(m_selectedActor->OccShape(), TopAbs_EDGE);
	int i = 0;
	while (explorer.More())
	{
		if (std::find(edgesToRoundCache.begin(), edgesToRoundCache.end(), TopoDS::Edge(explorer.Current())) != edgesToRoundCache.end())
		{
			edgeIndex.push_back(i);
		}
		i++;
		explorer.Next();
	}

	if (cacheActor)
	{
		m_mainWindow->OnActorConfirmCreate(cacheActor);
		cacheActor->m_featureShape.reset(new FeatureShapeMix(cacheActor->OccShape()));
	}

	if (m_selectedActor)
	{
		m_mainWindow->RemoveActorFromRenderer(m_selectedActor);
	}

	RoundOffOperation* op = new RoundOffOperation("倒圆角运算", shapeIndex, edgeIndex, _radius);
	m_mainWindow->GetHistoryManager()->applyOperation(op);

	close();
}
